package com.hbnu.Control;

import com.hbnu.Pojo.DormBuild;
import com.hbnu.Pojo.User;
import com.hbnu.Service.DormManagerService;
import com.hbnu.Service.Impl.DormManagerServiceImp;
import com.hbnu.Service.Impl.UserServiceImpl;
import com.hbnu.Service.UserService;
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/dormManager.action")
public class DormManagerServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private DormManagerService dormManagerService = new DormManagerServiceImp();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");

        String id = req.getParameter("id");

        if (null != action && "list".equals(action)) {

            String searchType = req.getParameter("searchType");
            String keyword = req.getParameter("keyword");


            List<User> userList = userService.findAllManager(searchType, keyword);

            req.setAttribute("searchType", searchType);
            req.setAttribute("keyword", keyword);

            req.setAttribute("users", userList);

            req.setAttribute("mainRight", "dormManagerList.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);

        }
        else if (null != action && "preAdd".equals(action)) {

            List<DormBuild> dormBuilds = dormManagerService.findBuilds();


            req.setAttribute("builds", dormBuilds);

            req.setAttribute("mainRight", "dormManagerAddOrUpdate.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
        }
        else if ("save".equals(action)) {
            String name = req.getParameter("name");
            String password  = req.getParameter("passWord");
            String sex = req.getParameter("sex");
            String tel = req.getParameter("tel");
            String[] dormId = req.getParameterValues("dormBuildId");

            // id 为点击修改
            if (null == id || id.isEmpty()) {
                User user = new User(name, password, sex, tel, null, 1);
                user.setDisabled(0);

                User user1 = (User) req.getSession().getAttribute("session_user");
                user.setCreateUserId(user1.getId());
                userService.savaManager(user, dormId);
            }
            else {
                User user = userService.findById(Integer.parseInt(id));
                user.setName(name);
                user.setPassword(password);
                user.setSex(sex);
                user.setTel(tel);

                userService.updateManager(user, dormId);


                dormManagerService.deleteByUserId(user.getId());
                dormManagerService.savaManagerAndBuild(user.getId(), dormId);

            }
            resp.sendRedirect(getServletContext().getContextPath() + "/dormManager.action?action=list");
        }
        else if ("preUpdate".equals(action)) {
            User user = userService.findById(Integer.parseInt(id));
            List<DormBuild> userbuilds = dormManagerService.findByUserId(user.getId());
            user.setDormBuilds(userbuilds);
            List<Integer> userBuildIds = new ArrayList<>();

            for (DormBuild dormBuild : userbuilds) {
                userBuildIds.add(dormBuild.getId());
            }

            List<DormBuild> dormBuilds = dormManagerService.findBuilds();


            req.setAttribute("builds", dormBuilds);


            req.setAttribute("userBuildids", userBuildIds);
            req.setAttribute("user", user);

            req.setAttribute("mainRight", "dormManagerAddOrUpdate.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
        } else if ("deleteOrAcive".equals(action)) {

            String disabled = req.getParameter("disabled");

            User user = userService.findById(Integer.parseInt(id));
            user.setDisabled(Integer.parseInt(disabled));

            userService.updateManager(user);

            resp.sendRedirect(getServletContext().getContextPath() + "/dormManager.action?action=list");

        }




    }
}
